package com.example.demo.controller;

import com.example.demo.dto.ApiResponse;
import com.example.demo.dto.UserInfoDTO;
import com.example.demo.dto.UserUpdateRequest;
import com.example.demo.entity.User;
import com.example.demo.entity.UserType;
import com.example.demo.service.FileService;
import com.example.demo.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;
    private final FileService fileService;

    @PostMapping
    public ResponseEntity<User> createUser(@RequestBody User user) {
        return ResponseEntity.ok(userService.createUser(user));
    }

    @PutMapping("/{id}")
    public ApiResponse<?> updateUser(@PathVariable Long id, @RequestBody User user) {
        return userService.updateUser(id, user);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    public ApiResponse<?> getUserById(@PathVariable Long id) {
        return ApiResponse.success(userService.getUserById(id));
    }

    @GetMapping
    public ResponseEntity<List<User>> getAllUsers() {
        return ResponseEntity.ok(userService.getAllUsers());
    }

    @GetMapping("/type/{userType}")
    public ResponseEntity<List<User>> getUsersByType(@PathVariable UserType userType) {
        return ResponseEntity.ok(userService.getUsersByType(userType));
    }

    @GetMapping("/college/{college}")
    public ResponseEntity<List<User>> getUsersByCollege(@PathVariable String college) {
        return ResponseEntity.ok(userService.getUsersByCollege(college));
    }

    @GetMapping("/check/username/{username}")
    public ResponseEntity<Boolean> checkUsernameExists(@PathVariable String username) {
        return ResponseEntity.ok(userService.existsByUsername(username));
    }

    @GetMapping("/check/student-id/{studentId}")
    public ResponseEntity<Boolean> checkStudentIdExists(@PathVariable String studentId) {
        return ResponseEntity.ok(userService.existsByStudentId(studentId));
    }

    @GetMapping("/check/teacher-id/{teacherId}")
    public ResponseEntity<Boolean> checkTeacherIdExists(@PathVariable String teacherId) {
        return ResponseEntity.ok(userService.existsByTeacherId(teacherId));
    }

    @GetMapping("/profile")
    public ApiResponse<?> getProfile(@AuthenticationPrincipal User user) {
        return ApiResponse.success(userService.getUserById(user.getId()));
    }

    @PutMapping("/profile")
    public ApiResponse<?> updateProfile(@AuthenticationPrincipal User user, @RequestBody UserUpdateRequest request) {
        return ApiResponse.success(userService.updateProfile(user.getUsername(), request));
    }

    @PostMapping("/avatar")
    public ApiResponse<?> updateAvatar(@AuthenticationPrincipal User user, @RequestParam("file") MultipartFile file) {
        return ApiResponse.success("头像更新成功", userService.updateAvatar(user.getId(), file));
    }

    @GetMapping("/info")
    public ApiResponse<UserInfoDTO> getUserInfo(Authentication authentication) {
        String username = authentication.getName();
        UserInfoDTO userInfo = userService.getUserInfo(username);
        return ApiResponse.success(userInfo);
    }
} 